package task

import (
	"encoding/json"
	"fmt"
	"github.com/robfig/cron"
	"guoquan/util"
	"log"
)

func InitTask() {
	log.Println("task start ....")

	c := cron.New()
	_ = c.AddFunc("*/5 * * * * *", func() {
		log.Println("tttttttttttttttt")
		getAllProduct()
	})

	c.Start()
}

type apiData struct {
	Code   int
	Result string
	// Msg string
	ErrorMsg string
	Data     string
}

func getAllProduct() {
	url := "http://www.baidu.com"
	param := make(map[string]interface{})
	param["centerId"] = 3261
	param["type"] = 1
	param["limit"] = 1
	param["page"] = 1

	paramJson, err := json.Marshal(param)
	if err != nil {
		fmt.Println("map to json err, ", err)
		return
	}
	postData := make(map[string]interface{})
	postData["ispInfoId"] = 10004
	postData["data"], err = util.Encrypt(string(paramJson))
	if err != nil {
		fmt.Println("加密错误", err)
		return
	}

	fmt.Println(postData)
	headerData := make(map[string]string)
	headerData["Content-Type"] = "application/json"
	result, errRes := util.HttpPost(url, postData, map[string]string{}, headerData)
	if errRes != nil {

	}
	fmt.Println(result)

	// 临时获返回值内容
	//resultMap := make(map[string]interface{})
	apidata := new(apiData)
	_ = json.Unmarshal([]byte(result), apidata)
	fmt.Println(apidata)
	/*func () {
		if code, ok := resultMap["code"]; ok {
			if code == 1 {
				fmt.Println("error")
			} else {
				if data, ok := resultMap["data"]; ok {
					decryptData := util.Decrypt(data.(string))
					fmt.Println(data)
					fmt.Println(decryptData)
				}
			}
		}
	}()*/
}
